package cn.addapp.ohospickers.util;

import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.math.RoundingMode;
import java.net.NetworkInterface;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

public class StringUtils {
  public static boolean isStringDataNull(String checkData) {
    if ("".equals(checkData) || checkData == null
        || "null".equals(checkData) || checkData == null) {
      return true;
    }
    return false;
  }

  public static boolean isListDataNull(List list) {
    if (list == null || list.size() <= 0) {
      return true;
    }
    return false;
  }

  public static String replaceBlankCharacter(String string) {
    if (string != null) {
      string = string.replace(" ", "");
      return string;
    }
    return null;
  }


  public static String getMacAddress() {
    String str = "";
    String macSerial = "";
    try {
      Process pp = Runtime.getRuntime().exec(
          "cat /sys/class/net/wlan0/address ");
      InputStreamReader ir = new InputStreamReader(pp.getInputStream());
      LineNumberReader input = new LineNumberReader(ir);

      for (; null != str; ) {
        str = input.readLine();
        if (str != null) {
          macSerial = str.trim();// 去空格
          break;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    if (macSerial == null || "".equals(macSerial)) {
      try {
        return loadFileAsString("/sys/class/net/eth0/address")
            .toUpperCase().substring(0, 17);
      } catch (Exception e) {
        e.printStackTrace();
      }

    }
    if (macSerial == null || "".equals(macSerial)) {
      try {
        List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface nif : all) {
          if (!nif.getName().equalsIgnoreCase("wlan0")) continue;

          byte[] macBytes = nif.getHardwareAddress();
          if (macBytes == null) {
            return "";
          }

          StringBuilder res1 = new StringBuilder();
          for (byte b : macBytes) {
            res1.append(String.format("%02X:", b));
          }

          if (res1.length() > 0) {
            res1.deleteCharAt(res1.length() - 1);
          }
          return res1.toString();
        }
      } catch (Exception ex) {
        macSerial = "02:00:00:00:00:00";
      }
    }
    return macSerial;
  }

  public static String loadFileAsString(String fileName) throws Exception {
    FileReader reader = new FileReader(fileName);
    String text = loadReaderAsString(reader);
    reader.close();
    return text;
  }

  public static String loadReaderAsString(Reader reader) throws Exception {
    StringBuilder builder = new StringBuilder();
    char[] buffer = new char[4096];
    int readLength = reader.read(buffer);
    while (readLength >= 0) {
      builder.append(buffer, 0, readLength);
      readLength = reader.read(buffer);
    }
    return builder.toString();
  }


  public static boolean isNull(String str) {
    if (str == null || str.equals("")) {
      return true;
    } else {
      return false;
    }
  }

  public static String get2XXDa(double data) {
    DecimalFormat df = new DecimalFormat("###.00");
    df.setGroupingSize(0);
    df.setRoundingMode(RoundingMode.UP);
    String d = df.format(data);

    if (d.indexOf(".") == 0) {
      d = "0" + d;
    }

    System.out.println(d);
    return d;
  }

  /**
   * 向下取整
   *
   * @param data
   * @return
   */
  public static String get2XXDown(double data) {
    DecimalFormat df = new DecimalFormat("###.00");
    df.setGroupingSize(0);
//        df.setRoundingMode(RoundingMode.DOWN);
    String d = df.format(data);
    if (d.indexOf(".") == 0) {
      d = "0" + d;
    }
    System.out.println(d);
    return d;
  }

  public static String get2XXDat(double data) {


    System.out.println(data);
    DecimalFormat df = new DecimalFormat("###.00");
    df.setGroupingSize(0);
    df.setRoundingMode(RoundingMode.DOWN);
    String d = df.format(data);

    if (d.indexOf(".") == 0) {
      d = "0" + d;
    }

    System.out.println(d);
    return d;
  }

  public static double get2XX(double data) {
    double f = data;
//
//		String s = String.valueOf(f);
//
//		s = s.substring(s.indexOf('.') + 1);
//
//		System.out.println(s.length());
//
//		if (s.length() > 2) {
//			data += 0.01;
//		}
//
//		System.out.println(data);
    DecimalFormat df = new DecimalFormat("###.00");

    df.setGroupingSize(0);
    df.setRoundingMode(RoundingMode.UP);
    String d = df.format(data);

    System.out.println(d);
    return Double.valueOf(d);
  }

  public static String getFormat_s1(long sd) {

    Date dat = new Date(sd);
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(dat);
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
        "yyyy-MM-dd HH:mm");
    String sb = format.format(gc.getTime());

    return sb;
  }

  public static String getFormatYYMMDD(long sd) {
    Date dat = new Date(sd);
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(dat);
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
        "yyyy-MM-dd");
    String sb = format.format(gc.getTime());

    return sb;
  }

  public static String getFormat(long sd) {

    Date dat = new Date(sd);
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(dat);
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");
    String sb = format.format(gc.getTime());
    return sb;
  }

  public static String getFormatWithoutSS(long sd) {

    Date dat = new Date(sd);
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(dat);
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
        "yyyy-MM-dd HH:mm");
    String sb = format.format(gc.getTime());
    return sb;
  }

  /**
   * 2月2日 下午17:49
   *
   * @param times
   * @return
   */
  public static String getFormatForAMPM(long times) {
    Calendar c = Calendar.getInstance();
    Date dat = new Date(times);
    c.setTime(dat);
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
        "MM月dd日");
    String sb = format.format(c.getTime());
    String morning = "  上午 ";
    String afternoon = "  下午 ";
    String amOrpm = "";
    int hour = 0;
    if (c.get(c.HOUR_OF_DAY) > 12) {
      amOrpm = afternoon;
      hour = c.get(c.HOUR_OF_DAY) - 12;
      return sb + amOrpm + hour + ":" + String.format("%02d", c.get(c.MINUTE));
    } else {
      amOrpm = morning;
      hour = c.get(c.HOUR_OF_DAY);
      return sb + amOrpm + String.format("%02d", hour) + ":" + String.format("%02d", c.get(c.MINUTE));
    }
  }

  public static String getFormatHHMMSS(long sd) {
    Date dat = new Date(sd);
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(dat);
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
        "HH:mm:ss");
    String sb = format.format(gc.getTime());
    return sb;
  }

  public static String getFormatHHMM(long sd) {
    Date dat = new Date(sd);
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(dat);
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
        "MM-dd HH:mm");
    String sb = format.format(gc.getTime());

    return sb;
  }

  public static String getOnlyHHMM(long sd) {
    Date dat = new Date(sd);
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(dat);
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
        "HH:mm");
    String sb = format.format(gc.getTime());

    return sb;
  }

  private final static long hourLevelValue = 60 * 60 * 1000;
  private final static long minuteLevelValue = 60 * 1000;
  private final static long secondLevelValue = 1000;

  /**
   * 剩余的小时,精确到分钟
   *
   * @param sd
   * @return
   */
  public static String getRemainHHMM(long sd) {
    String remainHM = "请等待上车";
    GregorianCalendar gc = new GregorianCalendar();
    long now = gc.getTimeInMillis();
    long remainMills = sd - now;
    if (remainMills >= 0) {
      int hour = (int) (remainMills / hourLevelValue);
      int minute = (int) ((remainMills - hour * hourLevelValue) / minuteLevelValue);
      int second = (int) ((remainMills - hour * hourLevelValue - minute * minuteLevelValue) / secondLevelValue);
      remainHM = getStringFromat02d(hour) + " : " + getStringFromat02d(minute) + " : " + getStringFromat02d(second);
    } else {
      remainHM = "请等待上车";
    }
    return remainHM;
  }

  /**
   * 数字带0两位
   *
   * @param num
   * @return
   */
  public static String getStringFromat02d(int num) {
    return String.format("%02d", num);
  }

  public static String getFormat1(long sd) {
    Date dat = new Date(sd);
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(dat);
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
        "yyyy.MM.dd", Locale.US);
    String sb = format.format(gc.getTime());

    return sb;
  }

  public static String getFormat3(long sd) {

    Date dat = new Date(sd);
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(dat);
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
        "MM-dd  HH:mm", Locale.CHINA);
    String sb = format.format(dat);

    return sb;
  }

  public static String getFormat4(long sd) {

    Date dat = new Date(sd);
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(dat);
    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
        "HH:mm", Locale.US);
    String sb = format.format(gc.getTime());

    return sb;
  }

  public static long getLastWeek() {
    // TODO Auto-generated method stub
    Calendar calendar = Calendar.getInstance();


    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
    calendar.set(Calendar.WEEK_OF_YEAR,
        calendar.get(Calendar.WEEK_OF_YEAR) - 1);
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    LogUtils.error("", "calendar=======" + calendar.getTimeInMillis() + "=====");

    return calendar.getTimeInMillis();

  }

  public static long getThisWeek() {
    // TODO Auto-generated method stub
    Calendar calendar = Calendar.getInstance();

    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
    calendar.set(Calendar.HOUR, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    LogUtils.error("", "calendar=======" + calendar.getTimeInMillis() + "=====");

    return calendar.getTimeInMillis();

  }

  private static String mYear;
  private static String mMonth;
  private static String mDay;
  private static String mWay;

  public static String StringData(String sd) {
    final Calendar c = Calendar.getInstance();
    c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
    mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
    mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
    mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
    mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
    if ("1".equals(mWay)) {
      mWay = "天";
    } else if ("2".equals(mWay)) {
      mWay = "一";
    } else if ("3".equals(mWay)) {
      mWay = "二";
    } else if ("4".equals(mWay)) {
      mWay = "三";
    } else if ("5".equals(mWay)) {
      mWay = "四";
    } else if ("6".equals(mWay)) {
      mWay = "五";
    } else if ("7".equals(mWay)) {
      mWay = "六";
    }
    return mYear + "-" + mMonth + "-" + mDay + "日" + " 星期" + mWay;
  }

  public static String getDateFors(long times) {
    final Calendar c = Calendar.getInstance();
    Date dat = new Date(times);
    c.setTime(dat);
    c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
    mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
    mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
    mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
    mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
    if ("1".equals(mWay)) {
      mWay = "天";
    } else if ("2".equals(mWay)) {
      mWay = "一";
    } else if ("3".equals(mWay)) {
      mWay = "二";
    } else if ("4".equals(mWay)) {
      mWay = "三";
    } else if ("5".equals(mWay)) {
      mWay = "四";
    } else if ("6".equals(mWay)) {
      mWay = "五";
    } else if ("7".equals(mWay)) {
      mWay = "六";
    }
    return " 星期" + mWay;
  }

  public static String getFormatYMDW(long times) {
    final Calendar c = Calendar.getInstance();
    Date dat = new Date(times);
    c.setTime(dat);
    c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
    mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
    mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
    mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
    mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
    if ("1".equals(mWay)) {
      mWay = "天";
    } else if ("2".equals(mWay)) {
      mWay = "一";
    } else if ("3".equals(mWay)) {
      mWay = "二";
    } else if ("4".equals(mWay)) {
      mWay = "三";
    } else if ("5".equals(mWay)) {
      mWay = "四";
    } else if ("6".equals(mWay)) {
      mWay = "五";
    } else if ("7".equals(mWay)) {
      mWay = "六";
    }
    return mYear + "-" + mMonth + "-" + mDay + "  星期" + mWay;
  }

  public static String getDateFors2(long times) {
    final Calendar c = Calendar.getInstance();
    Date dat = new Date(times);
    c.setTime(dat);
    c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
    mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
    mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
    mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
    mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
    if ("1".equals(mWay)) {
      mWay = "天";
    } else if ("2".equals(mWay)) {
      mWay = "一";
    } else if ("3".equals(mWay)) {
      mWay = "二";
    } else if ("4".equals(mWay)) {
      mWay = "三";
    } else if ("5".equals(mWay)) {
      mWay = "四";
    } else if ("6".equals(mWay)) {
      mWay = "五";
    } else if ("7".equals(mWay)) {
      mWay = "六";
    }
    return mWay;
  }

  public static String getDateMMSS(long times) {
    final Calendar c = Calendar.getInstance();
    Date dat = new Date(times);
    c.setTime(dat);
    c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));

    int sec = c.get(Calendar.MINUTE);
    if (c.get(Calendar.HOUR_OF_DAY) > 12) {
      return "下午" + c.get(Calendar.HOUR) + ":" + sec;
    } else {
      return "上午" + c.get(Calendar.HOUR) + ":" + sec;
    }
  }

  public static String getDateMMSS12(long times) {
    final Calendar c = Calendar.getInstance();
    Date dat = new Date(times);
    c.setTime(dat);
    c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
    int miao = c.get(Calendar.SECOND);
    int sec = c.get(Calendar.MINUTE);
    if (c.get(Calendar.HOUR_OF_DAY) > 12) {
      return "下午" + c.get(Calendar.HOUR) + ":" + sec + ":" + miao;
    } else {
      return "上午" + c.get(Calendar.HOUR) + ":" + sec + ":" + miao;

    }
  }
}
